#ifndef VL_FSTREAM_HPP
#define VL_FSTREAM_HPP

#include "stream.hpp"
#include <cstdio>

namespace vl
{

struct file_buf : public device_buf
{
	enum open_flags
	{
		in = (1 << 0),
		out = (1 << 1)
	};

	file_buf(char const* path, int flags = (in | out));

	file_buf(FILE* f)
	: f(f)
	{
	}
	
	virtual size_t read(byte* p, size_t size);

	virtual size_t write(byte const* p, size_t size);
	
	int error()
	{
		return !f;
	}

	void do_close();
	
	~file_buf();
	
	FILE* f;
};

struct ifstream : public istream
{
	ifstream(char const* path)
	: iobase(new file_buf(path, file_buf::in))
	{
	}

	ifstream(FILE* f)
	: iobase(new file_buf(f))
	{
	}
};


struct ofstream : public ostream
{
	ofstream(char const* path)
	: iobase(new file_buf(path, file_buf::out))
	{
	}

	ofstream(FILE* f)
	: iobase(new file_buf(f))
	{
	}

};

} // namespace vl

#endif // VL_FSTREAM_HPP
